package game.tileMap.model
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import game.tileMap.MapFactory;
	import game.tileMap.model.vo.MapData;
	import game.tileMap.model.vo.MapObj;
	
	import mk.gamemap.model.Player;
	
	
	public class TileMapModel extends EventDispatcher
	{
		
		
		
		public var view_arr:Array;
		public var tiles:Array;
		public var passage_arr:Array  
		public var mapData:MapData;
		public var objArr:Array;
		public var player:Player;
		
		public var maxX:int;
		public var maxY:int;
		public function TileMapModel()
		{
			init();
		}
		
		
		
		
		protected function init():void{
			objArr=[];
		}
		
		
		public function setup(mapId:int):void{

			mapData=MapFactory.getInstance().getMapData(1);
			maxX=mapData.width-1
			maxY=mapData.height-1;
			setupEvents()


		}
		
		public function setupEvents():void{
//			
			
			
			
			
		}
		
		
		
		public function setMapPos(x:int, y:int):void{
			
		}
		
		
		//位置XY是否允许通过
		public function passable(x:int, y:int, flag:int = 0x01):Boolean{
			if (x<0||y<0||x>maxX||y>maxY){
				return false;
			}
			if (!passableObj(x,y)){
				return false;
			}
			if (x>=0&&y>=0){
				var tile:int = mapData.data[x][y]     
				if (tile<3){
					return true
				}
					
				return false
				  
			}
			
			return true;
		}
		
		
		/**
		 *能否穿越物体 
		 * @param x
		 * @param y
		 * @return 
		 * 
		 */		
		public function passableObj(x:int,y:int):Boolean{
			for each(var mapSp:MapObj in objArr){
				if (mapSp.onPos(x,y)){
					return mapSp.through;
				}
			}
			
			return true;
		}
		
		
	}
}